/*
 * IkaArray.cpp
 */

#include "IkaArray.h"

using namespace atomic;

namespace atomic {

/*

// *********************************************************************
template<typename T>
IkaArray<T>::IkaArray() {
	// first alloc
	myRealSize = array_step;
	myArrayLenght = 0;
	myArray = (T*)malloc(sizeof(T)*myRealSize);
	if (myArray == NULL){
		throw FAIL;
	}
}

// *********************************************************************
template<typename T>
IkaArray<T>::~IkaArray() {
	if(myArray){
		free(myArray);
		myArray = 0;
	}
}
// *********************************************************************
template<typename T>
IkaArray<T>::IkaArray(const IkaArray<T> &array){
	// alloc with a Good Old Malloc!
	//myArray = (T*) malloc(sizeof(T) *array.myRealSize);

	if (myArray == NULL){
		throw FAIL;
	}

	// Good Old Memcopy!
	// memcpy(array, myArray, sizeof(T)*array.myRealSize);

	//sizes
	myRealSize = array.myRealSize;
	myArrayLenght = array.myArrayLenght;
}

// *********************************************************************
template<typename T>
IkaArray<T>& IkaArray<T>::operator=(const IkaArray<T> & array){
	// if already the same object
	if(this == &array){
		return *this;
	}

	// if the other array if empty
	if(array == 0){
		Clear;
	}

	memcopy forever
	memcpy(myArray, array.myArray, sizeof(T)*array.mySize );

	// size //
	SetSize(array.mySize);

	return *this;
}

// *********************************************************************
template <typename T>
unsigned int IkaArray<T>::Size(){
// www.anyexemple.com/programming/cplusplus/cplusplus_dynamic_array_template_typename.xml
	return myArrayLenght;
}

// *********************************************************************
template <typename T>
void IkaArray<T>::SetSize(unsigned int newSize){
	myArrayLenght = newSize;

	// if size > 0
	if(myArrayLenght != 0 ){
		// if new size is larger than before
		// or smaller than 0.5*before
		if(( myArrayLenght > myRealSize )||( myArrayLenght < myRealSize/2)){
			// realloc
			myRealSize = myArrayLenght;
			//TODO?
			//myArray = (T*)realloc(myArray, sizeof(T)*myArrayLenght);
			if (myArray==NULL){
				throw FAIL;
			}
		}
	} else {
		Clear();
	}
}

// *********************************************************************
template <typename T>
void IkaArray<T>::Delete(unsigned int position){
	// if only one element in array = easy way
	if(myArrayLenght==1){
		Clear();
	// else hard way, copy each element before "position" at place -1
	} else {
		for(unsigned int i=position; i<myArrayLenght; i++){
			myArray[i] = myArray[i+1];
		}
		// and decrease size
		myArrayLenght--;
	}
}

// *********************************************************************
template <typename T>
void IkaArray<T>::Clear(){
	myArrayLenght=0;
	// and reinit the allocation
	myArray = (T*)realloc(myArray, sizeof(T)*array_step);
	myRealSize = array_step;
}

// *********************************************************************
template <typename T>
void* IkaArray<T>::GetPtr(){
	return myArray;
}

// *********************************************************************
template <typename T>
T& IkaArray<T>::operator [](unsigned int index){
	return myArray[index];
}

// *********************************************************************
template <typename T>
void IkaArray<T>::Add(const T &object){
	myArrayLenght++;
	// if oversizing : realloc *2
	if(myArrayLenght > myRealSize){
		myRealSize *= array_mult;
		myArray = (T*)realloc(myArray, sizeof(T)*myRealSize);
		// if trouble
		if(myArray==NULL){
			throw FAIL;
		}
	}
	myArray[myArrayLenght-1] = object;
}
*/
}
